﻿namespace OleDbDAL.Comm
{
    using DBUtility;
    using IDal.Comm;
    using Model.Comm;
    using System;
    using System.Data;
    using System.Text;

    public class CommTypeApp : ICommType
    {
        private string dbname = "Systems";

        public void Add(CommTypeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_CommType(");
            builder.Append("ChainNum,CommTypeName,CommTypeRemark,CreateTime");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.CommTypeName + "',");
            builder.Append("'" + model.CommTypeRemark + "',");
            builder.Append("'" + model.CreateTime + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int CommTypeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_CommType ");
            builder.Append(" where CommTypeId=" + CommTypeId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_CommType ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by CommTypeId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "CommTypeId", "T_CommType", swhere);
        }

        public CommTypeEnt GetModel(int CommTypeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CommTypeId,ChainNum,CommTypeName,CommTypeRemark,CreateTime from T_CommType ");
            builder.Append(" where CommTypeId=" + CommTypeId);
            CommTypeEnt ent = new CommTypeEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.CommTypeId = CommTypeId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.CommTypeName = table.Rows[0]["CommTypeName"].ToString();
                ent.CommTypeRemark = table.Rows[0]["CommTypeRemark"].ToString();
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_CommType ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "CommTypeId");
        }

        public void Update(CommTypeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_CommType set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("CommTypeName='" + model.CommTypeName + "',");
            builder.Append("CommTypeRemark='" + model.CommTypeRemark + "',");
            builder.Append("CreateTime='" + model.CreateTime + "'");
            builder.Append(" where CommTypeId=" + model.CommTypeId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

